<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>编辑</title>
		<link rel="stylesheet" href="__AGENT__/css/css.css" />
		<script type="text/javascript" src="__PUBLIC__/static/js/jquery.min.js"></script>
		<link rel="stylesheet" href="__PUBLIC__/static/layui/css/layui.css">
		<script src="__PUBLIC__/static/layui/layui.js"></script>
		<script src="__PUBLIC__/static/js/area/area2.js"></script>
	</head>

	<style>
		.yanshi {
			height: 38px;
			line-height: 1.3;
			line-height: 38px\9;
			border: 1px solid #e6e6e6;
			background-color: #fff;
			border-radius: 2px;
			width: 211px;
		}
	</style>

	<body>
		<div style="width: 600px;height: 480px;position: fixed;">
			<form class="layui-form update_form">
				<input type="hidden" name="id" value="{$user.id}" />
                {if $Think.session.user_level==1}
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 200px;">省级代理商</label>
					<div class="layui-input-block" style="float: left; margin-right: 30px; margin-left: 0;">
						<select name="agency_id" id="agency_id" onchange="select_province(this);" lay-ignore class="yanshi">
							<option value="" >暂时留空</option>
							{volist name="province" id="tv"}
							<option value="{$tv.id}" {if condition="$user.agency_id eq $tv.id" }selected{/if}>{$tv.user_name}</option>
							{/volist}
						</select>
					</div>
				</div>
				{/if}
                {if $Think.session.user_level==1 || $Think.session.user_level==4}
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 200px;">市级代理商</label>
					<div class="layui-input-block" style="float: left; margin-right: 30px; margin-left: 0;">
						<select name="agency_id1" id="agency_id1" onchange="select_agency(this);" lay-ignore class="yanshi">
							{volist name="agency" id="str"}
							<option value="{$str.id}" {if condition="$user.agency_id1 eq $str.id" }selected{/if}>{$str.user_name}</option>
							{/volist}
						</select>
					</div>
				</div>
				{/if}
				{if $Think.session.user_level==1 || $Think.session.user_level==4 || $Think.session.user_level==2}
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 200px;">区域运营商</label>
					<div class="layui-input-block" style="float: left; margin-right: 30px; margin-left: 0;">
						<select name="agency_id2" id="agency_id2" lay-ignore class="yanshi">
							{volist name="area" id="vo"}
							<option value="{$vo.id}" {if condition="$user.agency_id2 eq $vo.id" }selected{/if}>{$vo.user_name}</option>
							{/volist}
						</select>
					</div>
				</div>
				{/if}
                <div class="layui-form-item">
						<label class="layui-form-label" style="width: 200px;">商家ID</label>
						<div class="layui-input-block update_shurk">
							<input type="text" id="agent" name="tj_agent" value="{$user.tj_agent}" required lay-verify="required" placeholder="请输入商家ID" autocomplete="off" class="layui-input" style="width: 200px;">
						</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 200px;">推荐人ID</label>
					<div class="layui-input-block update_shurk">
						<input type="text" id="level" name="level" value="{$user.level}" required lay-verify="required" placeholder="请输入推荐人ID" autocomplete="off" class="layui-input" style="width: 200px;">
					</div>
				</div>
				<div class="layui-form-item update_btnbox" style="position: absolute;left: 80px;">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit type="button" lay-filter="submit_from">立即提交</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
			</form>
		</div>

		<script>
			layui.use('form', function() {
				var form = layui.form;

				//监听表单提交
				form.on('submit(submit_from)', function(data) {
					var agency_id	= $("#agency_id").val();
					var agency_id1	= $("#agency_id1").val();
					var agency_id2 	= $("#agency_id2").val();
					var agent = $("#agent").val();
					var level = $("#level").val();

					if(agency_id1=='0'){
						layer.msg("请选择市代理");
						return false;
					}
					if(agency_id2=='0'){
						layer.msg("请选择区域运营商");
						return false;
					}
					if(agent==''){
						layer.msg("请输入商家ID");
						return false;
					}
					if(level==''){
						layer.msg("请输入推荐人ID");
						return false;
					}

					edit_agency();
					return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
				});
			});

			//提交表单
			function edit_agency() {
				$.ajax({
					type: "post",
					url: "{:url('accounts/edit_user')}",
					data: $('.layui-form').serialize(), //表单数据
					success: function(data) {
						if(data.code == 200) {
							layer.msg(data.msg, {
								icon: 1,
								time: 1500 //2秒关闭（如果不配置，默认是3秒）
							}, function() {
								var index = parent.layer.getFrameIndex(window.name); //获取窗口索引  
								parent.layer.close(index);
								window.parent.location.reload();
							});
						} else {
							layer.msg(data.msg, {
								icon: 1,
								time: 1500
							});
						}
					},
					error: function(data) {
						var a = data;
					}
				});

			}
			
			function select_province(obj){
				var agency_id=$(obj).val(); 
				var lay = layer.load(0, {shade: false,time: 1.5 * 1000});
				$.ajax({
						type: 'post',
						url: "{:url('accounts/get_agency_id')}",
						data: {
							agency_id:agency_id
						},
						success: function(data) {  
							var list=data.data;
							if(list){
								var html="<option value='0'>选择市代理</option>";
								for(var i=0;i<list.length;i++){
									var trade=list[i]; 
									html+="<option value="+trade['id']+">"+trade['user_name']+"</option>";
								}
								$("#agency_id1").html(html);
		
							}
						}
				})
			}

			function select_agency(obj){
				var agency_id1=$(obj).val(); 
				var lay = layer.load(0, {shade: false,time: 1.5 * 1000});
				$.ajax({
						type: 'post',
						url: "{:url('accounts/get_agency_id1')}",
						data: {
							agency_id1:agency_id1
						},
						success: function(data) {  
							var list=data.data;
							if(list){
								var html="<option value='0'>选择市代理</option>";
								for(var i=0;i<list.length;i++){
									var trade=list[i]; 
									html+="<option value="+trade['id']+">"+trade['user_name']+"</option>";
								}
								$("#agency_id2").html(html);
		
							}
						}
				})
			}
		</script>
	</body>

</html>